package com.hospital.demo.controller.lxy;



import com.hospital.demo.mapper.lxy.*;
import com.hospital.demo.model.lxy.Cbc;
import com.hospital.demo.model.lxy.Patients;
import com.hospital.demo.model.lxy.Certificate;
import com.hospital.demo.model.lxy.Workers;
import com.hospital.demo.service.lxy.CertificateService;
import com.hospital.demo.util.ResultUtils;
import com.hospital.demo.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李欣远
 * @since 2020-04-08
 */
@RestController
@RequestMapping("/hospital/certificate")
public class CertificateController {
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private CertificateMapper certificateMapper;
    @Autowired
    private CbcMapper cbcMapper;
    @Autowired
    private PatientsMapper patientsMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private WorkersMapper workersMapper;
    @Autowired
    JavaMailSenderImpl mailSender;
    @RequestMapping("/cers")
    public ResultVO list(Model model){
        List<Certificate> cers= certificateService.selCer();
        model.addAttribute("cers",cers);
        cers.forEach(System.out::println);
        return ResultUtils.success(cers);
    }
    @RequestMapping("/cers0")
    public ResultVO list0(Model model){
        List<Certificate> cers= certificateService.selCer0();
        model.addAttribute("cers",cers);
        cers.forEach(System.out::println);
        return ResultUtils.success(cers);
    }
    @RequestMapping("/cbcs")
    public ResultVO listCbc(Model model){
        List<Cbc> cbcs= cbcMapper.selectList();
        model.addAttribute("cbcs",cbcs);
        cbcs.forEach(System.out::println);
        return ResultUtils.success(cbcs);
    }
    @RequestMapping("/cers/{did}")
    public List<Certificate> listD(@PathVariable("did")Integer did, Model model){
        List<Certificate> certificate= certificateService.selCerByD(did);
        model.addAttribute("cers",certificate);
        return certificate;
    }
    @GetMapping("/searchCer")
    public ResultVO searchCer(Integer cdid) {
        Certificate certificate = new Certificate();
        certificate.setCid(cdid);

        List<Certificate> list = certificateService.selCerByD(cdid);
        return ResultUtils.success(list);
    }
    @GetMapping("/searchCerList")
    public ResultVO searchCerList(Integer cdeid) {
        Certificate certificate = new Certificate();
        if (cdeid != 0) {
            certificate.setCdeid(cdeid);
        }
        List<Certificate> list = certificateService.selCerByDe(certificate);
        return ResultUtils.success(list);
    }
    @GetMapping("/searchCerName")
    public ResultVO searchCerName(String pname) {
        Certificate certificate = new Certificate();

        List<Certificate> list = certificateMapper.selCerByPName(pname);
        return ResultUtils.success(list);
    }
//    @RequestMapping("/cers/{deid}")
//    public List<Certificate> listDe(@PathVariable("deid")Integer deid,Model model){
//        List<Certificate> cers= certificateService.selCerByDe(deid);
//        model.addAttribute("cers",cers);
//        return cers;
//    }
//    @GetMapping("/cerde")
//    public List<Department> toAddpageDe(Model model){
//        QueryWrapper<Department> wrapper = new QueryWrapper<>();
//        wrapper.select("dname");
//        List<Department> departments=departmentMapper.selectList(wrapper);
//        model.addAttribute("departments",departments);
//        return departments;
//    }
//    @GetMapping("/cerd")
//    public List<Workers> toAddpageDo(Model model){
//        QueryWrapper<Workers> wrapper = new QueryWrapper<>();
//        wrapper.select("dname");
//        List<Workers> workers=workersMapper.selectList(wrapper);
//        model.addAttribute("workers",workers);
//        return workers;
//    }
    @PostMapping("/cer")
    public ResultVO<Certificate> addCer(Certificate certificate){
        Patients patients = patientsMapper.selectById(certificate.getCpid());
        Workers workers = workersMapper.selectById(certificate.getCdid());
        Date date = new Date();
        certificate.setDatec(date);
        String pmail= patients.getPemail();
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("诊断书更新通知");
        mailMessage.setText(patients.getPname()+"您好，您的诊断书"+certificate.getCid()+"号已被"+workers.getDname()+"医生添加，请注意查看");

        mailMessage.setTo(pmail);
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        return ResultUtils.success(certificateService.save(certificate));
    }
//    @GetMapping("/reg/{cid}")
//    public String toUpdateCer(@PathVariable("cid")Integer cid, Model model){
//        Certificate certificateById=certificateService.getById(cid);
//        model.addAttribute("certificateById",certificateById);
//        QueryWrapper<Department> wrapper1 = new QueryWrapper<>();
//        wrapper1.select("dname");
//        List<Department> departments=departmentMapper.selectList(wrapper1);
//        model.addAttribute("departments",departments);
//        QueryWrapper<Workers> wrapper2 = new QueryWrapper<>();
//        wrapper2.select("dname");
//        List<Workers> workers=workersMapper.selectList(wrapper2);
//        model.addAttribute("workers",workers);
//        return "reg/update";
//    }
    @PostMapping("/updateCer")
    public ResultVO updateCer(Certificate certificate){
//        certificateService.updateById(certificate);
        Patients patients = patientsMapper.selectById(certificate.getCpid());
        Workers workers = workersMapper.selectById(certificate.getCdid());
        String pmail= patients.getPemail();
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("诊断书更新通知");
        mailMessage.setText(patients.getPname()+"您好，您的诊断书"+certificate.getCid()+"号已被"+workers.getDname()+"医生更新，请注意查看");

        mailMessage.setTo(pmail);
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
//        SimpleMailMessage mailMessage = new SimpleMailMessage();
//        mailMessage.setSubject("标题");
//        mailMessage.setText("正文");
//
//        mailMessage.setTo("1214624372@qq.com");
//        mailMessage.setFrom("1346520072@qq.com");
//        mailSender.send(mailMessage);
        return ResultUtils.success(certificateService.updateById(certificate));
    }
    @PostMapping("/delCer")
    public ResultVO deleteCer(@RequestParam(name = "cid", defaultValue = "-1")Integer cid){

        return ResultUtils.success(certificateService.removeById(cid));
    }

}

